package com.moss.cloud.base.api.model.from;

import com.moss.cloud.common.core.constant.Regular;
import com.moss.cloud.common.core.exception.SystemErrorType;
import com.moss.cloud.common.core.utils.BooleanHandel;
import com.moss.cloud.common.core.utils.GenUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;

@Slf4j
@Data
public class SysUserForm implements Serializable {
    private Long id;
    /**
     * 用户名
     */
    @Size(min = 6, max = 32, message = "用户名最小6位，最大32位")
    @Pattern(regexp = Regular.LETTER_NUMBER_REG,message = "用户名必须是数字和字母组成")
    private String username;
    /**
     * 用户密码密文
     */
    @Size(min = 6, max = 64, message = "密码最小6位，最大64位")
    private String password;
    /**
     * 用户姓名
     */
    @NotBlank(message = "用户昵称不能为空")
    @Size(max = 32, message = "用户昵称最大32位")
    private String name;
    /**
     * 用户手机
     */
    @Size(min = 6, max = 11, message = "用户手机号最小6位，最大11位")
    private String mobile;
    /**
     * 简介
     */
    private String description;
    /**
     * 用户邮箱
     */
    @Email(regexp = Regular.EMAIL_REG, message = "邮箱格式错误")
    private String email;
    /**
     * 租户ID
     */
    private Long tenantId;
    /**
     * 用户头像
     */
    @Size(max = 225, message = "头像最大长度不能超过225位")
    @Pattern(regexp = Regular.IMG_REG,message = "头像格式错误")
    private String avatar;
    /**
     * 用户性别（0男，1女，2未知，3双性）
     */
    @Min(value = 0,message = "性别格式错误")
    @Max(value = 3,message = "性别格式错误")
    private Integer sex;
    /**
     * openId
     */
    private String openId;
    /**
     * 用角色列表
     */
    private List<Long> roleIds;
    /**
     * 部门ID
     */
    private Long deptId;

    /** 生日*/
    private LocalDate birthday;

    public static void checkFrom(SysUserForm sysUserForm) {
        Boolean userName = StringUtils.isBlank(sysUserForm.getUsername());
        Boolean mobile = StringUtils.isBlank(sysUserForm.getMobile());
        Boolean email = StringUtils.isBlank(sysUserForm.getEmail());
        Boolean openId = StringUtils.isBlank(sysUserForm.getOpenId());
        BooleanHandel.isTrue(userName).trueHandle(() -> {
            sysUserForm.setUsername(GenUtil.strNo());
            log.info("userName为空生成账号:{}", sysUserForm.toString());
        });
        BooleanHandel.trueThrow(userName && mobile && email && openId).throwMessage(SystemErrorType.ONE_OF_THEM_EMPTY);
    }
}
